﻿using System;
using System.Web;
using FFIV.Lib.BusinessRules;
using FFIV.Lib.IView.Articles;
using FFIV.Lib.Utilities;

namespace FFIV.Lib.Controlers.Admin
{
	public class AddArticleController
	{
		private readonly IAddArticle m_View;

		public AddArticleController(IAddArticle view)
		{
			m_View = view;
		}

		public void LoadPage()
		{
			m_View.AuthorName = UsersUtility.GetCurrentUserName() ?? "";
			m_View.Categories = CategoriesUtility.GetCategories();
		}

		public void AddArticle()
		{
			Guid? userId = UsersUtility.GetCurrentUserId();
			if (userId.HasValue)
			{
				int index = ArticlesRules.AddArticle(userId.Value, m_View.AuthorName, m_View.CategoryId, m_View.Title, m_View.Summary, m_View.Content, m_View.Tag, 0, false);
				HttpContext.Current.Response.Redirect(UrlUtility.GetArticleDetailUrl(index));
			}
		}

		public void AddDraft()
		{
			Guid? userId = UsersUtility.GetCurrentUserId();
			if (userId.HasValue)
			{
				int index = ArticlesRules.AddArticle(userId.Value, m_View.AuthorName, m_View.CategoryId, m_View.Title, m_View.Summary, m_View.Content, m_View.Tag, 0, true);
				HttpContext.Current.Response.Redirect(UrlUtility.GetArticleDetailUrl(index));
			}
		}
	}
}
